import React, { useState, useEffect } from 'react'
import { DatePicker } from 'antd'
import moment from 'moment'
import { strToNum } from '@src/assets/js/tool'

function Main ({ value, onChange, config = {} }) {
  const [c, setC] = useState({})
  useEffect(() => {
    const o = { ...config }
    delete o.historyOnly
    setC(o)
  }, [config])
  return (
    <DatePicker value={value} onChange={onChange} {...c} style={{ width: '100%' }} />
  )
}

export async function formatValue (v) {
  if (v instanceof moment) {
    return Math.floor(v.valueOf() / 1000)
  }
  return undefined
}

export function formatInitValue (v, config = {}) {
  v = strToNum(v)
  if (v) {
    return moment(v * 1000)
  }
}

export default Main
